#!/bin/bash
# author tuyb


function isgreen() {
    if [ $? -eq 0 ]; then
        echo -e "\e[32m$1==========>succeed\e[0m"
    else
        echo -e "\e[31m$1==========>failed\e[0m"
        exit 1
    fi
}

#判断是否存在公钥,不存在就创建
[ ! -f /root/.ssh/id_rsa.pub ] && ssh-keygen -P '' -f /root/.ssh/id_rsa >/dev/null 2>&1 || echo 1>/dev/null 2>&1
isgreen "创建公钥"

pwd="Iamadm1n!!"

# 获取文件中IP地址
ipList=$(grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' /etc/hosts | grep -Ev '^127\.')
isgreen "获取IP地址列表"
for ip in $ipList
do
    sshpass -p "$pwd" ssh-copy-id -o StrictHostKeyChecking=no "$ip" >/dev/null 2>&1
    isgreen "Secret free login $ip "
done